<?php
declare(strict_types=1);
namespace App\System\Controller;
use App\System\ErrorCode\CError;
use App\System\Exception\CException;
use App\System\Logic\HeiUserLogic;
use Haozing\FastCore\Abstract\AbstractController;
use Haozing\FastCore\Annotation\Permission;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Qbhy\HyperfAuth\Annotation\Auth;

#[Controller]
class LoginController extends AbstractController
{
    #[Inject]
    protected HeiUserLogic $heiUserLogic;

    /**
     * 登录
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[RequestMapping(path: '/system/login',methods: 'post')]
    public function login(): ResponseInterface
    {
        // 验证参数
        $params = $this->request->all();
        $validator = $this->validationFactory->make($params,
            [
                'username' => 'required',
                'password' => 'required',

            ],
            [
                'username.required' => '用户名不能为空',
                'password.required' => '密码不能为空',

            ]
        );

        if ($validator->fails()){
            throw new CException(CError::$params_invalid,$validator->errors()->first());
        }
        // 执行业务逻辑
        $data = $this->heiUserLogic->login($params);

        return $this->success($data);
    }

    /**
     * 获取用户信息
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[RequestMapping(path: '/system/user/info',methods: 'get')]
    #[Auth(value: 'jwt')]
    #[Permission]
    public function info(): ResponseInterface
    {
        return $this->success($this->heiUserLogic->info());
    }
}